package com.gxufl.dao;

import java.util.List;

import com.gxufl.Application;
import com.gxufl.entity.Room;
import com.gxufl.entity.Seat;
import com.gxufl.entity.Ticket;
import com.gxufl.util.JdbcUtil;

public class SeatDao {
	public List<Seat> findList(String roomname,Boolean isuse){
		//List<Notice> list =new ArrayList<Notice>();
		//1.构造sql语句
		//2.调用dbutil查询sql结果
		String sql = "select * from movie_seat where 1";

		if (roomname != null && !roomname.isEmpty()) {
			sql = sql + " and roomname = '" + roomname + "'";
		}

		if (isuse != null) {
			sql += " and isuse = " + (isuse ? 1 : 0);
		}

		return JdbcUtil.query(sql, Seat.class);
		
	}
	// 
	public Seat findOneByCode(String roomname,String seatcode ) {
		String sql = "select * from movie_ticket where 1 and seatcode ='"+seatcode+"' and roomname ='"+roomname+"'";		
		List<Seat> list = JdbcUtil.query(sql, Seat.class);
		if(!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
	//保存方法
	public void save(List<Seat> list) {

		boolean isOK = true;

		for (int i = 0, ii = list.size(); i < ii; i++) {
			isOK = isOK && JdbcUtil.save(list.get(i));
		}

		if (isOK) {
			System.out.println("\n"+"座位保存成功"+"\n");
			Application.fn显示主菜单();
		} else {
			System.out.println("\n"+"座位保存失败"+"\n");
			Application.fn显示主菜单();
		}
	}

	public void save(Seat seat) {
		boolean isOK = JdbcUtil.save(seat);

		if (isOK) {
			System.out.println("保存成功");
		} else {
			System.out.println("保存失败");
		} 
	}

	//在保存票据前，验证票据有效的逻辑
	public boolean checkBeforeSaveTicket(Ticket ticket) {
		
		//String seatcode=ticket.seatcode;
		//String roomname=ticket.roomname;
		Seat seat=findOneByCode(ticket.roomname,ticket.seatcode);
		
		if (seat ==null) {
			return true;
//			if (!seat.isuse) {
//				return true;
//			}
		}
		return false;
		
	}
	
	
}
